<template>
  <div class="operation-page">
    <h2>协议管理</h2>
    <div class="button-group">
      <el-button type="primary" @click="addProtocol">新增协议</el-button>
      <el-button @click="goBack">返回</el-button>
    </div>
    <el-table :data="protocols" style="width: 100%">
      <el-table-column prop="name" label="协议名称" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button @click="editProtocol(row)">编辑</el-button>
          <el-button @click="deleteProtocol(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="协议" v-model="protocolDialogVisible" width="30%">
      <el-form>
        <el-form-item label="协议名称">
          <el-input
            v-model="currentProtocolName"
            placeholder="请输入协议名称"
          />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-button @click="protocolDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveProtocol">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

const protocols = ref([
  { name: "协议1" },
  { name: "协议2" },
  { name: "协议3" },
]);

const protocolDialogVisible = ref(false);
const currentProtocolName = ref("");
const isEditMode = ref(false);

const addProtocol = () => {
  isEditMode.value = false;
  currentProtocolName.value = "";
  protocolDialogVisible.value = true;
};

const editProtocol = (protocol) => {
  isEditMode.value = true;
  currentProtocolName.value = protocol.name;
  protocolDialogVisible.value = true;
};

const saveProtocol = () => {
  if (isEditMode.value) {
    // 编辑逻辑
    console.log("编辑协议:", currentProtocolName.value);
  } else {
    // 新增逻辑
    console.log("新增协议:", currentProtocolName.value);
    protocols.value.push({ name: currentProtocolName.value });
  }
  protocolDialogVisible.value = false;
};

const deleteProtocol = (protocol) => {
  // 删除逻辑
  console.log("删除协议:", protocol.name);
  protocols.value = protocols.value.filter((p) => p.name !== protocol.name);
};

const goBack = () => {
  router.back();
};
</script>

<style scoped>
.operation-page {
  padding: 20px;
}

.button-group {
  margin-bottom: 20px;
}

.el-table {
  margin-top: 20px;
}
</style> 